/*
 * NewOrder.java
 *
 * Created on March 26, 2007, 6:30 PM
 */



package edu.byu.isys413.jjenkins.view;
import edu.byu.isys413.jjenkins.data.*;
import java.text.NumberFormat;
import java.util.List;
import javax.swing.DefaultListModel;
import javax.swing.JOptionPane;

/**
 * This form is called to create or view an order.
 * @author byu
 */
public class PickTransLineGUI extends javax.swing.JFrame {
    
    TransactionBO trans;
    
    //This is used so that other classes can access the transactionline that was selected in the selection process used for returns and repairs
    public TransactionLineBO selectedLine;
    // Holds transaction lines on the GUI
    DefaultListModel model = new DefaultListModel();
    
    List<TransactionLineBO> li;
    
    private static PickTransLineGUI instance = null;
    
    
    /** Creates new form NewOrder */
    private PickTransLineGUI() {
        initComponents();
        this.pack();
        lstLines.setModel(model);
                this.setLocationRelativeTo(null);
        
        
    }
    
    
    /**
     * gets a single of this class
     */
    public static synchronized PickTransLineGUI getInstance() {
        if (instance == null) {
            instance = new PickTransLineGUI();
        }
        // I choose to not do a singleton pattern here because this screen is used for several puposes that may need to run at the same time
        
        
        
        return instance;
    }
    
    
    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    // <editor-fold defaultstate="collapsed" desc=" Generated Code ">//GEN-BEGIN:initComponents
    private void initComponents() {
        jPanel4 = new javax.swing.JPanel();
        jPanel12 = new javax.swing.JPanel();
        btnDelete = new javax.swing.JButton();
        jScrollPane1 = new javax.swing.JScrollPane();
        lstLines = new javax.swing.JList();

        getContentPane().setLayout(new java.awt.FlowLayout());

        setTitle("Pick Transaction Line");
        jPanel4.setLayout(new java.awt.BorderLayout());

        jPanel4.setPreferredSize(new java.awt.Dimension(800, 300));
        jPanel12.setLayout(new java.awt.FlowLayout(java.awt.FlowLayout.LEFT));

        btnDelete.setText("Select");
        btnDelete.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnDeleteActionPerformed(evt);
            }
        });

        jPanel12.add(btnDelete);

        jPanel4.add(jPanel12, java.awt.BorderLayout.CENTER);

        jScrollPane1.setBorder(javax.swing.BorderFactory.createTitledBorder("Items On Transaction"));
        lstLines.setBackground(new java.awt.Color(224, 223, 227));
        jScrollPane1.setViewportView(lstLines);

        jPanel4.add(jScrollPane1, java.awt.BorderLayout.NORTH);

        getContentPane().add(jPanel4);

        pack();
    }// </editor-fold>//GEN-END:initComponents
    
    
    /**
     * This method removes a tranasactionline from the transaction. It does not remove the transaction line form the database
     * @param evt
     */
    private void btnDeleteActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnDeleteActionPerformed
        
        selectedLine = (TransactionLineBO)trans.getTransactionList().get(lstLines.getSelectedIndex());
        this.dispose();
        
        
    }//GEN-LAST:event_btnDeleteActionPerformed
    
    


    
    
    
    /**
     * This method populates the JList used to hold the transaction lines
     */
    public void populateTransactionLines(){
        try{
            NumberFormat currency = NumberFormat.getCurrencyInstance();
            trans.endTransaction();
            model.removeAllElements();
            
            
            // List of all interesets
            li =  trans.getTransactionList();
            
            
            // creaetes entries
            for(int i = 0; i < li.size(); i++){
                TransactionLineBO line =li.get(i);
                String description = "-";
                String quan = "-";
                String coupon = "-";
                
                //checks to see if there is a revenue source
                description = line.getRevenueSource().getDescription();
                
                quan = line.getRevenueSource().getQuantity() + "";
                double price = line.getSubtotal();
                
                
                // This add space onto the description to have uniform formatting
                while(description.length()<35){
                    description += " ";
                }
                
                //checks to see if there is a coupon
                if(line.getCouponID() != null){
                    coupon = currency.format(line.getCoupon().getAmount());
                }
                String strLine  =description + "           quantity:  " + quan +   "        coupon:  " + coupon + "         price:  " + currency.format(price);
                
                model.addElement(strLine);
                
                this.setVisible(true);
            }
        }catch(DataException ex){
            JOptionPane.showMessageDialog(null,ex.toString(),"Error adding item to View", JOptionPane.ERROR_MESSAGE);
        }
        
        
    }
    /**This is used to select a product from a revenuSource
     *
     * param: TransactionBO transaction with the product
     */
    public void selectProduct(TransactionBO transaction){
        trans = transaction;
        // Adds the items to the GUI in a list
        populateTransactionLines();
        
    }
    
    
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton btnDelete;
    private javax.swing.JPanel jPanel12;
    private javax.swing.JPanel jPanel4;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JList lstLines;
    // End of variables declaration//GEN-END:variables
    
}
